package com.fitbet.admin.service.bet.impl;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fitbet.admin.dao.BetDao;
import com.fitbet.admin.dao.UserBetDao;
import com.fitbet.admin.dto.BetRequest;
import com.fitbet.admin.handler.SmsHandler;
import com.fitbet.admin.service.DataService;
import com.fitbet.admin.service.bet.BetService;
import com.fitbet.admin.service.impl.DataServiceImpl;
import com.fitbet.admin.web.Constant;
import com.fitbet.entity.bet.Bet;
import com.fitbet.entity.bet.UserBet;
import com.fitbet.entity.bet.UserBetSupport;
/**
 * 
 * @author sjh
 *
 */
@Service("betService")
public class BetServiceImpl extends DataServiceImpl<Bet> implements BetService {

	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Dao
	private BetDao betDao;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<Bet> betService;
	
	@Autowired
	private SmsHandler SmsHandler;
	
	@Dao
	private UserBetDao userBetDao;
	
	@Value("#{config['wechat.host']}")
	private String wechatHost;
	
	
	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.BetService#count()
	 */
	public int count() {
		
		return betService.count(Bet.class);
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.BetService#list()
	 */
	public List<Bet> list(Page page) {
		
		return betService.listByPage(Bet.class, page);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.BetService#create(com.fitbet.entity.bet.Bet)
	 */
	public void create(BetRequest betRequest) throws ParseException {
		
		Bet bet = betRequest.generateBet();
		genericDaoSupport.save(bet);
	}


	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.BetService#open(java.lang.Long)
	 */
	public void open(Long id) {
		
		Bet bet = genericDaoSupport.load(Bet.class, id);
	
		genericDaoSupport.update(bet.openBet());
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.BetService#close(java.lang.Long)
	 */
	public void close(Long id) {
		
		Bet bet = genericDaoSupport.load(Bet.class, id);
		
		genericDaoSupport.update(bet.closeBet());
		
		List<UserBet> userBets = betDao.listById(bet.getId());
		
		for(UserBet userBet:userBets) {
			
			if(userBet.isSuccess()) {
				
				String content ="【"+Constant.SMS_COMPANY_NAME+"】"+"恭喜您！在FITBET平台上参与的"+bet.getName()+"活动挑战成功，并获得奖金"+200+"元人民币，详情请登录 "+wechatHost+"/profile "+"查询。";
			  
				SmsHandler.sendMessage(userBet.getUser().getMobile(), content);
			   
			    List<UserBetSupport> userBetSupports = userBetDao.listByIdAndSupportIsTrue(userBet.getId());
			  
			    for(UserBetSupport userBetSupport:userBetSupports) {
			    	
			    		String contents ="【"+Constant.SMS_COMPANY_NAME+"】"+"恭喜您！在FITBET平台上参与的"+bet.getName()+"活动挑战成功，并获得奖金"+300+"元人民币，详情请登录 "+wechatHost+"/profile "+"查询。";
					    SmsHandler.sendMessage(userBetSupport.getUser().getMobile(), contents);
			    }
			} else {
				
				List<UserBetSupport> userBetSupports = userBetDao.listByIdAndSupportIsFalse(userBet.getId());
				
				  for(UserBetSupport userBetSupport:userBetSupports) {
				    	
				    	String contents ="【"+Constant.SMS_COMPANY_NAME+"】"+"恭喜您！在FITBET平台上参与的"+bet.getName()+"活动挑战成功，并获得奖金"+300+"元人民币，详情请登录 "+wechatHost+"/profile "+"查询。";
						SmsHandler.sendMessage(userBetSupport.getUser().getMobile(), contents);
				    	
				    }
			}
			
		
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.BetService#listById(java.lang.Long)
	 */
	public List<UserBet> listById(Long id) {
		
		return betDao.listById(id);
	}

	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.service.bet.BetService#editCover(java.lang.Long)
	 */
	public void editCover(Long id, String media) {
		
		Bet bet = genericDaoSupport.load(Bet.class, id);
		
		genericDaoSupport.update(bet.editCover(media));
		
		
	}

	


}
